import 'dart:typed_data';

import 'package:bluetooth_protocols_plugin/src/base/integrated/integrated_realtime_info_base.dart';

// 综合实时信息
abstract class LorinIntegratedRealtimeInfo extends IntegratedRealtimeInfoBase {
  // 数据格式
  int dataFormat = 1;

  // Battery and power information
  int? busVoltage; // 当前电池总电压 (0.01V)
  int? busCurrent; // 当前电池输出总电流 (0.01A)
  int? chargeVoltage; // 充电口电压 (0.01V)
  int? chargeCurrent; // 充电总电流 (0.01A)
  int? speed; // 当前车速 (0.01km/h)
  int? acce; // 行驶加速度 (0.01km/h/s)
  int? torque; // 电机输出扭矩 (0.01N/m)
  int? outputRate; // 输出矢量电压百分比 (0.01%)
  int? batteryOutputPower; // 电池输出功率 (W)
  int? motorOutputPower; // 电机输出功率 (W)

  int? batteryPercentage; // 剩余电量 (0.01%)
  int? batteryPercentageForRide; // 剩余可行驶的电量百分比 (0.01%)
  int? estimatedTotalMileage; // 预计满电可行驶里程 (0.01km)
  int? rtOverSpeedTiltBackThreshold; // 实时超速翘板阈值 (0.01km/h)
  int? rtOverSpeedWarningThreshold; // 实时超速报警阈值 (0.01km/h)

  int? mosTemperature; // 散热器温度 (°C, 偏置80)
  int? motorTemperature; // 电机温度 (°C, 偏置80)
  int? boardTemperature; // 主板高温区温度 (°C, 偏置80)
  int? lteSignal; // LTE信号强度 (0-4)

  int? maxSpeed; // 最高速度 (0.01km/h)
  int? maxPower; // 最大总功率 (W)
  int? maxMotorPower; // 最大电机输出功率 (W)

  // Single ride statistics
  int? singleMileage; // 本次里程 (0.01km)
  int? singleConsumption; // 本次消耗能量 (0.01KJ)
  int? singleRecovery; // 本次回收能量 (0.01KJ)
  int? singleRideTime; // 本次骑行时间 (S)
  int? singlePowerOnTime; // 本次开机时间 (S)

  // Total statistics
  int? totalMileage; // 总里程 (0.01km)
  int? totalConsumption; // 总消耗能量 (0.01KJ)
  int? totalRecovery; // 总回收能量 (0.01KJ)
  int? totalRideTime; // 总骑行时间 (S)
  int? totalPowerOnTime; // 总开机时间 (S)

  LorinIntegratedRealtimeInfo.fromBytes(Uint8List bytes)
      : super.fromBytes(bytes) {
    if (bytes.length < minLength) {
      throw ArgumentError(
          '字节数组长度不足，无法解析 $runtimeType，期望长度至少为 $minLength，实际长度为 ${bytes.length}');
    }
    var byteData = ByteData.sublistView(bytes);
    dataFormat = byteData.getInt16(0, Endian.little);
    busVoltage = byteData.getInt16(2, Endian.little);
    busCurrent = byteData.getInt16(4, Endian.little);
    chargeVoltage = byteData.getInt16(6, Endian.little);
    chargeCurrent = byteData.getInt16(8, Endian.little);
    speed = byteData.getInt16(10, Endian.little);
    acce = byteData.getInt16(12, Endian.little);
    torque = byteData.getInt16(14, Endian.little);
    outputRate = byteData.getInt16(16, Endian.little);
    batteryOutputPower = byteData.getInt16(18, Endian.little);
    motorOutputPower = byteData.getInt16(20, Endian.little);

    batteryPercentage = byteData.getUint16(22, Endian.little);
    batteryPercentageForRide = byteData.getUint16(24, Endian.little);
    estimatedTotalMileage = byteData.getInt16(26, Endian.little);
    rtOverSpeedTiltBackThreshold = byteData.getUint16(28, Endian.little);
    rtOverSpeedWarningThreshold = byteData.getUint16(30, Endian.little);

    mosTemperature = byteData.getInt8(32) + 80; // 偏置80
    motorTemperature = byteData.getInt8(33) + 80; // 偏置80
    boardTemperature = byteData.getInt8(34) + 80; // 偏置80
    lteSignal = byteData.getUint8(35);

    maxSpeed = byteData.getUint16(36, Endian.little);
    maxPower = byteData.getUint16(38, Endian.little);
    maxMotorPower = byteData.getUint16(40, Endian.little);

    singleMileage = byteData.getUint16(42, Endian.little);
    singleConsumption = byteData.getUint32(44, Endian.little);
    singleRecovery = byteData.getUint32(48, Endian.little);
    singleRideTime = byteData.getUint32(52, Endian.little);
    singlePowerOnTime = byteData.getUint32(56, Endian.little);

    totalMileage = byteData.getUint32(60, Endian.little);
    totalConsumption = byteData.getUint32(64, Endian.little);
    totalRecovery = byteData.getUint32(68, Endian.little);
    totalRideTime = byteData.getUint32(72, Endian.little);
    totalPowerOnTime = byteData.getUint32(76, Endian.little);
  }
}
